package tw.com.chttl;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TimePicker;
import android.widget.Toast;

public class TakePhotoActivity extends Activity{
	ImageView myImageView;
	EditText etDateTxt;
	EditText etTimeTxt;
	EditText etImageTxt;
	Button btnOK;
	Button btnCancel;
	private int myYear, myMonth, myDay, myHour, myMinute;
	static final int ID_DATEPICKER = 0;
	static final int ID_TIMEPICKER = 1;
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.take_photo_layout);
		
		myImageView = (ImageView)findViewById(R.id.takePhotoImageView);
		
		//let the build-in provider take photos for us
		//Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		//startActivityForResult(intent, 0);
		
		etDateTxt=(EditText)findViewById(R.id.etDate);
		etTimeTxt=(EditText)findViewById(R.id.etTime);
		etImageTxt=(EditText)findViewById(R.id.etImage);
		btnOK=(Button)findViewById(R.id.btnOK);  
		btnCancel=(Button)findViewById(R.id.btnCancel);
		btnOK.setOnClickListener(new Button.OnClickListener()  
	    {  
	            public void onClick(View v) {
	            	TakePhotoActivity.this.finish();
	            }
	    });  
		btnCancel.setOnClickListener(new Button.OnClickListener()  
	    {  
	            public void onClick(View v) {  
	            	TakePhotoActivity.this.finish();
	            }
	    });  
		etDateTxt.setOnTouchListener(new View.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				 final Calendar c = Calendar.getInstance();
				 myYear = c.get(Calendar.YEAR);
				 myMonth = c.get(Calendar.MONTH);
				 myDay = c.get(Calendar.DAY_OF_MONTH);
				showDialog(ID_DATEPICKER);
				return false;
			}
		});
		etTimeTxt.setOnTouchListener(new View.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				final Calendar c = Calendar.getInstance();
			    myHour = c.get(Calendar.HOUR_OF_DAY);
			    myMinute = c.get(Calendar.MINUTE);
			    showDialog(ID_TIMEPICKER);
				return false;
			}
		});
		
		 final Calendar c = Calendar.getInstance();
		 myYear = c.get(Calendar.YEAR);
		 myMonth = c.get(Calendar.MONTH);
		 myDay = c.get(Calendar.DAY_OF_MONTH);
		 myHour = c.get(Calendar.HOUR_OF_DAY);
		 myMinute = c.get(Calendar.MINUTE);
	
		 updateDateDisplay();
		 updateTimeDisplay();
		 updateTitleDisplay();
		
		 
	}
	
	 protected Dialog onCreateDialog(int id) {
		  // TODO Auto-generated method stub
		  switch(id){
		   case ID_DATEPICKER:
		    return new DatePickerDialog(this,
		      myDateSetListener,
		      myYear, myMonth, myDay);
		   case ID_TIMEPICKER:
			    return new TimePickerDialog(this,
			      myTimeSetListener,
			      myHour, myMinute, true);
		   default:
		    return null;
		    
		  }
		  
	 }
	 
	 private void updateDateDisplay() {
		 etDateTxt.setText(
	            new StringBuilder()
	                    // Month is 0 based so add 1
	            		.append(myYear).append("-")
	                    .append(myMonth+1).append("-")
	                    .append(myDay)
	                    );
	    }
	 private void updateTimeDisplay() {
		 etTimeTxt.setText(
	            new StringBuilder()		           
	            		.append(myHour).append(":")
	                    .append(myMinute)
	    );
	 }
	 private void updateTitleDisplay() {
		 etImageTxt.setText(
	            new StringBuilder()
		            .append(myYear).append("-")
	                .append(myMonth+1).append("-")
	                .append(myDay).append(" ")
	        		.append(myHour).append(":")
	                .append(myMinute)
	    );
	}
	 
	 private DatePickerDialog.OnDateSetListener myDateSetListener
	  = new DatePickerDialog.OnDateSetListener(){

		public void onDateSet(android.widget.DatePicker view, int year,
				int monthOfYear, int dayOfMonth) {
			myYear=year;
			myMonth=monthOfYear;
			myDay=dayOfMonth;
			// TODO Auto-generated method stub
			updateDateDisplay();
			
		}
	};
	 
	 private TimePickerDialog.OnTimeSetListener myTimeSetListener
	  = new TimePickerDialog.OnTimeSetListener(){
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			myHour=hourOfDay;
			myMinute=minute;
			updateTimeDisplay() ;
			
			
		}
	 };
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode == RESULT_OK){
			Bundle extras = data.getExtras();
			Bitmap bmp = (Bitmap) extras.get("data");
			myImageView.setImageBitmap(bmp);

		}
		else if(resultCode == RESULT_CANCELED){
			//end this activity
			TakePhotoActivity.this.finish();
		}
	}
}
